home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d939.lha / ExtraCmds / source_etc.lha / src / Common.c < prev    next >
C/C++ Source or Header  |  1993-10-22  |  6KB  |  215 lines

  1. /*   ---------------------------------      -------     
  2.  *   |\  | | | | |  |.| |   \|  |/ /|\      |||||||     
  3.  *   | | | |/  | |\ |/  |/|  |\ |/  |    ?  ---+---  =< 
  4.  *   | | | |   | |  |     |  |  |   |     \qqqqqqqqq/   
  5.  *   ---------------------------------  ~~~~~~~~~~~~~~~~
  6.  *  COMMON - Selects or Rejects lines common to two sorted files
  7.  *  Copyright (C) 1992 Torsten Poulin
  8.  *
  9.  *  This program is free software; you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation; either version 2 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program; if not, write to the Free Software
  21.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  *
  23.  *  The author can be contacted by s-mail at
  24.  *    Torsten Poulin
  25.  *    Banebrinken 99, 2, 77
  26.  *    DK-2400 Copenhagen NV
  27.  *    DENMARK
  28.  *
  29.  * $Log:    Common.c,v $
  30.  * Revision 37.5  93/02/11  21:36:03  Torsten
  31.  * Now using dos.library/CheckSignal()
  32.  * instead of exec.library/SetSignal().
  33.  * 
  34.  * Revision 37.4  93/02/07  11:41:04  Torsten
  35.  * Updated the copyright tag.
  36.  * 
  37.  * Revision 37.3  93/02/05  11:23:31  Torsten
  38.  * checked in with -k by Torsten at 93.02.05.11.24.04.
  39.  * 
  40.  */
  41.  
  42. #include <exec/types.h>
  43. #include <exec/libraries.h>
  44. #include <dos/dos.h>
  45. #include <dos/dostags.h>
  46. #include <proto/exec.h>
  47. #include <proto/dos.h>
  48. #include <string.h>
  49. #include "common_rev.h"
  50.  
  51. #define MAXLEN (512+1)
  52. #define TEMPLATE "FILE1/A,FILE2,-1/S,-2/S,-3/S"
  53. #define OPT_FILE1 0
  54. #define OPT_FILE2 1
  55. #define OPT_NOT1  2
  56. #define OPT_NOT2  3
  57. #define OPT_NOT3  4
  58.  
  59. char const versionID[] = VERSTAG;
  60. char const copyright[] = "$COPYRIGHT:© 1990,1992,1993 Torsten Poulin$";
  61.  
  62. LONG Common(struct DosLibrary *DOSBase,
  63.             BPTR f1, BPTR f2, BOOL print1, BOOL print2, BOOL print3);
  64.  
  65.  
  66. LONG entrypoint(VOID)
  67. {
  68.     struct RDArgs     *args;
  69.     struct DosLibrary *DOSBase;
  70.     struct Library    *SysBase;
  71.     LONG  arg[5];
  72.     LONG  rc = RETURN_OK;
  73.     BPTR  f1, f2;
  74.     UBYTE *name1, *name2;
  75.     BOOL  print1, print2, print3;
  76.     
  77.     SysBase = *(struct Library **) 4L;
  78.     if(!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  79.         return RETURN_FAIL;
  80.  
  81.     arg[OPT_FILE1] = arg[OPT_FILE2] = 0L;
  82.     arg[OPT_NOT1] = arg[OPT_NOT2] = arg[OPT_NOT3] = 0L;
  83.     
  84.     if(args = ReadArgs(TEMPLATE, arg, NULL))
  85.     {
  86.         name1 = (UBYTE *) arg[OPT_FILE1];
  87.         name2 = (UBYTE *) arg[OPT_FILE2];
  88.  
  89.         print1 = ! (BOOL) arg[OPT_NOT1];
  90.         print2 = ! (BOOL) arg[OPT_NOT2];
  91.         print3 = ! (BOOL) arg[OPT_NOT3];
  92.         
  93.         if(!(f1 = Open(name1, MODE_OLDFILE)))
  94.         {
  95.             LONG err = IoErr();
  96.             PrintFault(err, name1);
  97.             rc = RETURN_ERROR;
  98.         }
  99.         
  100.         if(!arg[OPT_FILE2])
  101.             f2 = Input();
  102.         else if(!(f2 = Open(name2, MODE_OLDFILE)))
  103.         {
  104.             LONG err = IoErr();
  105.             PrintFault(err, name2);
  106.             Close(f1);
  107.             rc = RETURN_ERROR;
  108.         }
  109.         
  110.         if(f1 && f2)
  111.         {
  112.             rc = Common(DOSBase, f1, f2, print1, print2, print3);
  113.             Close(f1);
  114.             if(arg[OPT_FILE2])
  115.                 Close(f2);
  116.         }
  117.         FreeArgs(args);
  118.     }
  119.     else
  120.     {
  121.         LONG err = IoErr();
  122.         PrintFault(err, "Common");
  123.         rc = RETURN_ERROR;
  124.     }
  125.     CloseLibrary((struct Library *) DOSBase);
  126.     return rc;
  127. }
  128.  
  129.  
  130. LONG Common(struct DosLibrary *DOSBase,
  131.             BPTR f1, BPTR f2, BOOL print1, BOOL print2, BOOL print3)
  132. {
  133.     UBYTE *stat1, *stat2;
  134.     UBYTE line1[MAXLEN], line2[MAXLEN];
  135.     LONG  cmp_result;
  136.     LONG  err;
  137.     BPTR  out = Output();
  138.     
  139.     stat1 = FGets(f1, line1, MAXLEN);
  140.     stat2 = FGets(f2, line2, MAXLEN);
  141.     
  142.     while(stat1 != 0 && stat2 != 0)
  143.     {
  144.         cmp_result = strcmp(line1, line2);
  145.         if(cmp_result == 0)
  146.         {
  147.             if(print3)
  148.             {
  149.                 if(print1) FPutC(out, '\t');
  150.                 if(print2) FPutC(out, '\t');
  151.                 PutStr(line1);
  152.             }
  153.             stat1 = FGets(f1, line1, MAXLEN);
  154.             stat2 = FGets(f2, line2, MAXLEN);
  155.         }
  156.         else if(cmp_result < 0)
  157.         {
  158.             if(print1)
  159.                 PutStr(line1);
  160.             stat1 = FGets(f1, line1, MAXLEN);
  161.         }
  162.         else
  163.         {
  164.             if(print2)
  165.             {
  166.                 if(print1) FPutC(out, '\t');
  167.                 PutStr(line2);
  168.             }
  169.             stat2 = FGets(f2, line2, MAXLEN);
  170.         }
  171.         
  172.         if (CheckSignal(SIGBREAKF_CTRL_C))
  173.         {
  174.             PrintFault(ERROR_BREAK, NULL);
  175.             return RETURN_WARN;
  176.         }
  177.     }
  178.     if(err = IoErr())
  179.     {
  180.         PrintFault(err, "Common");
  181.         return RETURN_ERROR;
  182.     }
  183.  
  184.     /* Output the remainder of the longest file */
  185.     if(stat1 || stat2)
  186.     {
  187.         if(print1 && stat1)
  188.             PutStr(line1);
  189.         if(print2 && stat2)
  190.         {
  191.             if(print1)
  192.                 FPutC(out, '\t');
  193.             PutStr(line2);
  194.         }
  195.         while(FGets(stat1 ? f1 : f2, line1, MAXLEN))
  196.         {
  197.             if(print1 && print2 && stat2)
  198.                 FPutC(out, '\t');
  199.             if(stat1 ? print1 : print2)
  200.                 PutStr(line1);
  201.  
  202.             if (CheckSignal(SIGBREAKF_CTRL_C))
  203.             {
  204.                 PrintFault(ERROR_BREAK, NULL);
  205.                 return RETURN_WARN;
  206.             }
  207.         }
  208.         if(err = IoErr())
  209.         {
  210.             PrintFault(err, "Common");
  211.             return RETURN_ERROR;
  212.         }
  213.     }
  214. }
  215.